package com.itheima.StudentSystem;

import java.util.ArrayList;
import java.util.Scanner;

//学生管理系统
public class StudentSystemTest {
    public static void main(String[] args) {
        ArrayList<StudentSystem> list = new ArrayList<>();
        printmenu();
        Scanner sc = new Scanner(System.in);

        while (true){
            int num = sc.nextInt();
            switch (num){
                case 1:
                    addStudent(list);
                    printmenu();
                    break;
                case 2:
                    System.out.println("请输入id");
                    int i = sc.nextInt();
                    deleteStudent(list,i);
                    printmenu();
                    break;
                case 3:
                    System.out.println("请输入id");
                    int k = sc.nextInt();
                    changeStudent(list,k);
                    printmenu();
                    break;
                case 4:
                    inquireStudent(list);
                    printmenu();
                    break;
                case 5:
                    return;
                default:
                    System.out.println("输入错误，请重新输入");
                    printmenu();
            }
        }
    }
    public static void printmenu(){
        System.out.println("---------------欢迎来到黑马学生管理系统---------------");
        System.out.println("1.添加学生");
        System.out.println("2.删除学生");
        System.out.println("3.修改学生");
        System.out.println("4.查询学生");
        System.out.println("5.退出");
        System.out.println("请输入您的选择");
    }

    public static void addStudent( ArrayList<StudentSystem> list){
        StudentSystem s1=new StudentSystem();
        Scanner sc = new Scanner(System.in);
        int id;
        while(true){
            System.out.println("输入id");
            id = sc.nextInt();
            //判断ID是否重复
            boolean fale = checkId(list, id);
            if(fale){
                System.out.println("输入失败，请重新输入");
            }else{
                break;
            }
        }
        System.out.println("输入name");
        String name = sc.next();
        System.out.println("输入age");
        int age = sc.nextInt();
        System.out.println("输入live");
        String live = sc.next();
        //把数据添加到对象里面 
        s1.setId(id);
        s1.setName(name);
        s1.setAge(age);
        s1.setLive(live);
        //把对象添加到集合里
        list.add(s1);
        System.out.println("添加成功");
    }
    public static void deleteStudent(ArrayList<StudentSystem> list,int id){
        for (int i = 0; i < list.size(); i++) {
            int id1 = list.get(i).getId();
            if(id1==id){
                list.remove(i);
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("用户不存在,删除失败");
    }
    public static void changeStudent(ArrayList<StudentSystem> list,int id){
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < list.size(); i++) {
            int id1 = list.get(i).getId();
            if(id1==id){
                System.out.println("输入name");
                String name = sc.next();
                System.out.println("输入age");
                int age = sc.nextInt();
                System.out.println("输入live");
                String live = sc.next();

                StudentSystem change = list.get(i);
                change.setName(name);
                change.setAge(age);
                change.setLive(live);
                return;
            }
        }
        System.out.println("用户不存在,修改失败");
    }
    public static void inquireStudent(ArrayList<StudentSystem> list){
        if(list.size()==0){
            System.out.println("当前无学生信息，请稍后查询");
            return;
        }
        System.out.println("id\t姓名+\t年龄\t住址");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getId()+"\t"+list.get(i).getName()+"\t"+list.get(i).getAge()+"\t"+list.get(i).getLive());
        }
    }
    public static boolean checkId(ArrayList<StudentSystem> list,int id){
        for (int i = 0; i < list.size(); i++) {
            int id1 = list.get(i).getId();
            if(id1==id) {
                return true;
            }
        }
        return false;
    }

}
